package com.zrrd.learn.chapter06数组;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 算法题
 *
 * @author wrn
 * @version 1.0
 * @date 2023/7/17 8:30
 */

public class Algorithm01 {

    public static void question1(int[] array){
        System.out.printf("您输入的数组为" + Arrays.toString(array));
        Arrays.sort(array);
        System.out.println("排序后的数组" + Arrays.toString(array));


        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            int num = 0;
            for (int j = i; j < array.length; j++) {
                if (array[j] > 0) {
                    if (array[i] != array[j]) {
                        num = j;
                        if (num==1){
                            sum+=array[i];
                        }
                        break;
                    }
                }
            }

            System.out.printf("当前数组里的%d,有%d个\n", array[i], num == 0 ? array.length - i : num - i);
            i = (num == 0) ? array.length - 1 : num - 1;
        }
        System.out.println("当前数组中唯一元素的和为"+sum);
    }

    public static void main(String[] args) {

        System.out.println("请输入5个数：");
        Scanner in = new Scanner(System.in);
        int[] array = new int[5];
        for (int i = 0; i < array.length; i++) {
            array[i] = in.nextInt();
        }
        question1(array);
    }
}
